package com.axend.aerosense.user.services;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.base.bean.BaseDevInfoBean;
import com.axend.aerosense.base.bean.BaseStringBean;
import com.axend.aerosense.base.impl.IComplete;
import com.axend.aerosense.base.livedatabus.LiveDatabus;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.bean.UserInfo;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.services.ILoginService;
import com.axend.aerosense.common.services.config.ServicesConfig;
import com.axend.aerosense.common.ui.LoadingDialog;
import com.axend.aerosense.common.util.JsonUtil;
import com.axend.aerosense.network.EasyHttp;
import com.axend.aerosense.network.cache.model.CacheMode;
import com.axend.aerosense.network.callback.ProgressDialogCallBack;
import com.axend.aerosense.network.exception.ApiException;
import com.axend.aerosense.network.subsciber.IProgressDialog;
import com.axend.aerosense.user.R;

/**
 * 应用模块:
 * <p>
 * 类描述: 用户登录信息服务类 为各个组件提供app登录信息
 * <p>
 */
@Route(path = ServicesConfig.User.LONGING_SERVICE, name = "登录服务")
public class ILoginServiceImpl implements ILoginService {

    @Override
    public boolean isLogin() {
        String tmp = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
        if(null != tmp && !TextUtils.isEmpty(tmp)){
            return true;
        }
        return false;
    }

    @Override
    public String getToken() {
        String tmp = MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN);
        if(null != tmp && !TextUtils.isEmpty(tmp)){
            return tmp;
        }
        return null;
    }

    @Override
    public String getUUID() {
        return null;
    }

    @Override
    public void refreshUserDetailInfo() {

    }

    @Override
    public void login() {
        ARouter.getInstance()
                .build(RouterActivityPath.Login.PAGE_LOGIN_HOME)
                .navigation();
    }

    @Override
    public void login(boolean isMainAccountLogin) {

    }

    @Override
    public void logout(Activity activity, IComplete listence) {
        EasyHttp.post(UrlApi.URL.Logout.getUrl())
                .headers("token", getToken())
                .cacheMode(CacheMode.NO_CACHE)
                .upJson(JsonUtil.toJson(new BaseDevInfoBean()))
                .execute(new ProgressDialogCallBack<BaseStringBean>(LoadingDialog.getProgressDialog(activity)) {
                    @Override
                    public void onTokenExpire() {
                        dismissProgress();
                        onSuccess(null);
                    }

                    @Override
                    public void onError(ApiException e) {
                        dismissProgress();
                        com.blankj.utilcode.util.ToastUtils.showShort(e.getMessage());
                    }

                    @Override
                    public void onSuccess(BaseStringBean bean) {
                        dismissProgress();
                        //此处目前是为一个废对象不做处理的
                        MmkvHelper.getInstance().getMmkv().encode(Gobal.SAVE_KEY.TOKEN, "");
                        MmkvHelper.getInstance().getMmkv().encode(Gobal.SAVE_KEY.IMAGE_ICON,"");
                        LiveDatabus.getInstance().with(Gobal.EVENT_KEY.USR_INFO_CHANGE, UserInfo.class).postValue(new UserInfo());
                        if(null != listence){
                            listence.complete();
                        }
                    }
                });
    }

    @Override
    public UserInfo getUserInfo() {
        UserInfo userInfo = MmkvHelper.getInstance().getMmkv().decodeParcelable(Gobal.SAVE_KEY.USR_INFO, UserInfo.class);
        return userInfo;
    }

    @Override
    public void onLoginCancel() {

    }

    @Override
    public void onTokenExpire() {
        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", false).greenChannel().navigation();
    }

    @Override
    public void init(Context context) {
    }
}
